package com.enjoy.book.dao;

import com.enjoy.book.bean.User;
import com.enjoy.book.util.DBHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 创建了一个QueryRunner对象，负责对用户表的操作
 */
public class UserDao {

    //创建QueryRunner对象
    QueryRunner runner = new QueryRunner();
    public User getUser(String name, String pwd) throws SQLException {

        // 1. 调用DBHelper获取连接对象
        Connection conn = DBHelper.getConnection();

        // 2. 编写待执行的sql语句
        String sql="select * from user where name=? and pwd=? and state = 1 ";

        // 3. 调用查询方法，将查询到的数据封装成User对象
        User user = runner.query(conn,sql,new BeanHandler<User>(User.class),name,pwd);

        // 4. 关闭连接对象
        conn.close();

        // 5. 返回user
        return user;
    }

    /**
     * 修改密码
     * @param id  需要修改密码的用户编号
     * @param pwd  新的密码
     * @return  修改的数据行
     */
    public int modifyPwd(long id,String pwd) throws SQLException {

        // 修改密码需要修改数据库，这里是待执行的改密sql语句
        String sql="update  user set pwd = ? where id=?";

        // 创建数据库连接对象
        Connection conn = DBHelper.getConnection();

        // 调用QueryRunner对象的修改方法
        int count = runner.update(conn,sql,pwd,id);

        // 关闭链接
        DBHelper.close(conn);

        // 返回count
        return count;
    }

    // UserDao测试类
    public static void main(String[] args){
        User user = null;
        try {
            user = new UserDao().getUser("super","123");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println(user);
    }
}

